<!-- 搜索食物、运动项目 -->
<template>
<div class="container">
    <!-- 搜索框 -->
    <div class="cancel" @click="$router.go(-1)">返回</div>
    <div class="search_box">
        <van-field v-model="keyword" clearable placeholder="输入关键词搜索" @clear="showListFlag=false;" @input="listenKw" />
    </div>
    <div class="search" @click="goSearch">搜索</div>
    <!-- 搜索历史 -->
    <div class="history_search" v-show="!showListFlag">
        <div style="font-size: 0.875rem;color: #888;padding-bottom: 1.25rem;">历史搜索</div>
        <div class="clear_history" @click="clearHistory">
            <van-icon name="delete" color="#e88e61" />
            清空历史
        </div>
        <div>
            <span @click="chooseSearch(text)" v-for="(text,index) in historySearchList" :key="index">{{ text }}</span>

        </div>
    </div>
    <!-- 列表 -->
    <div class="list" v-show="showListFlag">
        <van-empty v-show="dataList.length==0" description="没有相关数据哦~" />
        <ul class="sport_list" v-show="dataList.length>0">
            <li v-for="(item,index) in dataList" :key="item.id">
                <img :src="item.imgUrl" alt="">
                <div class="food_des">
                    <div>{{ item.name }}</div>
                    <div>{{item.cal}}千卡 / {{ item.unit }}{{ item.unitText }}</div>
                </div>
                <van-icon name="add" color="#66ca98" size="20" @click="addItem(item)" />
            </li>
        </ul>
    </div>

    <!-- 记录食物、运动数量详情的组件 -->
    <recorditem v-if="showAddItem" :item="choosed" @changeShow="changeShow" />

</div>
</template>

<script>
import {
    apiRequest
} from '../../assets/js/axios_api';
import recorditem from 'components/record_item.vue'

export default {
    metaInfo() {
        return {
            title: '搜索'
        }
    },
    name: 'Search',
    data() {
        return {
            userInfo: {},
            keyword: '',
            historySearchList: [],
            dataList: [],
            showListFlag: false,
            type: '',
            showAddItem: false,
            choosed: {},
        }
    },
    components: {
        recorditem
    },
    mounted() {
        this.type = this.$route.query.type == 'food' ? 'historyFoodSearch' : 'historySportSearch';
        this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
        this.historySearchList = JSON.parse(localStorage.getItem(this.type)) || [];
    },
    methods: {
        // 调用接口模糊搜索
        checkoutData() {
            this.showListFlag = true;
            let param = {
                limit: 2000,
                page: 1,
            }
            let url = '';
            if (this.type == 'historyFoodSearch') {
                url = 'food/page'
                param.foodName = this.keyword
            } else {
                url = 'sport/page'
                param.sportName = this.keyword
            }
            apiRequest(this, url, param, resData => {
                this.dataList = [];
                // 返回成功要做的事
                if (this.type == 'historyFoodSearch') {
                    resData.list.forEach(element => {
                        this.dataList.push({
                            id: element.id,
                            name: element.foodName,
                            unit: element.weight,
                            cal: element.foodTrophy,
                            imgUrl: element.foodIconUrl,
                            unitText: 'g',
                            fat: element.fat,
                            protein: element.protein,
                            carbohydrates: element.carbohydrates,
                        })
                    });
                } else {
                    resData.list.forEach(element => {
                        this.dataList.push({
                            id: element.id,
                            name: element.sportName,
                            unit: element.duration,
                            cal: element.sportTrophy,
                            imgUrl: element.sportIconUrl,
                            unitText: '分钟'
                        })
                    });
                }
            }, 'get')
        },
        // 点击添加
        addItem($this) {
            this.choosed = {
                id: $this.id,
                itemName: $this.name,
                itemCal: $this.cal,
                itemUnit: $this.unit,
                unitText: $this.unitText,
                imgUrl: $this.imgUrl,
                type: '',
            }
            if (this.type == 'historyFoodSearch') {
                this.choosed.type = 'food_' + this.$route.query.time
                this.choosed.fat = $this.fat //蛋白质
                this.choosed.protein = $this.protein //脂肪
                this.choosed.carbohydrates = $this.carbohydrates //碳水化合物
            } else {
                this.choosed.type = 'sport'
            }
            this.showAddItem = true;
        },
        changeShow(flag) {
            this.showAddItem = flag;
        },
        // 点击搜索
        goSearch() {
            this.keyword = this.keyword.trim();
            if (this.keyword) {
                this.pushHistSearch();
                this.showListFlag = true;
                this.checkoutData();
            }
        },
        // 从历史搜索中点击搜索
        chooseSearch(text) {
            this.keyword = text;
            this.checkoutData();
        },
        // 将搜索关键词保存在历史搜索中
        pushHistSearch() {
            if (this.historySearchList.indexOf(this.keyword) < 0) {
                this.historySearchList.unshift(this.keyword);
                localStorage.setItem(this.type, JSON.stringify(this.historySearchList))
            }
        },
        // 清空历史
        clearHistory() {
            this.historySearchList = [];
            localStorage.removeItem(this.type)
        },
        // 输入框内容监听，若清空则展示搜索历史，隐藏列表
        listenKw(val) {
            if (!val) {
                this.showListFlag = false;
            }
        },

    }
}
</script>

<style lang="less" scoped>
@import '../../assets/css/common.less';

.container {
    background-color: #fafafa;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

    .search_box {
        position: absolute;
        top: 1rem;
        left: 3.75rem;
        right: 5rem;
        margin-bottom: 2rem;

        .van-cell.van-field {
            padding: 0.375rem 0.75rem;
            border-radius: 1.25rem;
            border: 0.0625rem solid @font-color;
            font-size: 0.75rem;
        }
    }

    .cancel {
        position: absolute;
        left: 1.125rem;
        top: 1.625rem;
        color: #888;
        font-size: 0.8125rem;
    }

    .search {
        position: absolute;
        right: 1.125rem;
        top: 1.5rem;
        color: @font-color;
        font-size: 0.875rem;
    }

    .history_search {
        position: absolute;
        top: 5rem;
        left: 1rem;
        right: 1rem;

        .clear_history {
            position: absolute;
            right: 0;
            top: 0;
            background-color: #fff;
            padding: 0.25rem 0.625rem;
            border-radius: 1.25rem;
            color: #888;
        }

        span {
            padding: 0.625rem 0.9375rem;
            display: inline-block;
            color: #555;
            font-size: 0.8125rem;
        }
    }

    .list {
        background-color: #fafafa;
        position: absolute;
        top: 5rem;
        left: 1rem;
        right: 1rem;
        bottom: 3.75rem;

        .sport_list {
            width: 100%;
            height: 100%;
            overflow: scroll;

            li {
                background-color: #fff;
                padding: 0.625rem;
                margin-bottom: 0.625rem;
                border-radius: 0.5rem;
                position: relative;

                img {
                    width: 2.5rem;
                    height: 2.5rem;
                }

                .food_des {
                    display: inline-block;
                    vertical-align: bottom;

                    div {
                        padding: 0.1875rem 1.875rem;
                        font-size: 0.625rem;
                    }
                }

                .van-icon-add {
                    position: absolute;
                    right: 1rem;
                    top: 1.25rem;
                }
            }
        }
    }

}
</style>
